package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Api("菜品管理模块")
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    @ApiOperation("新增菜品")
    @Transactional
    public void save(DishDTO dishDTO) {
        log.info("存到到dish表和口味表中");
        List<DishFlavor> flavors = dishDTO.getFlavors();
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.save(dish);
        Long dishId = dish.getId();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
        }
        dishFlavorMapper.save(flavors);
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        log.info("查询到的数据为：{}", page);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Dish> findByCategoryId(Integer categoryId) {
        return dishMapper.findByCategoryId(categoryId);
    }

    @Override
    @Transactional
    public void delete(Long[] ids) {
        //do 1 启售中菜品不能删除 如果删除 就抛出异常
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            Integer status = dish.getStatus();
            if (status.equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //do 2 被套餐关联的菜品不能删除 如果删除 就抛出异常
        log.info("开始判断有无菜品关联");
        Integer[] counts = setmealDishMapper.countById(ids);
        log.info("count:{}",counts);
        for (Integer count : counts) {
            if (count!=0){
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
            }
        }
        //do 3 删除菜品后 还要去菜品口味表中 删除 相关口味数据
        for (Long id : ids) {
            dishMapper.delete(id);
            dishFlavorMapper.delete(id);
        }
    }

    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        log.info("主体添加成功");
        //删除原有口味信息
        dishFlavorMapper.delete(dishDTO.getId());
        log.info("删除成功");
        //重新插入口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null || flavors.size()!=0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.save(flavors);
        }
        log.info("口味添加成功");
    }

    @Override
    public DishVO findById(Long id) {
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.findById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void status(Integer status, Long id) {
        Dish dish = dishMapper.getById(id);
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }


}
